package com.itheima.controller.backend;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.backend.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 检查组管理
 */
@RestController
@RequestMapping( "/checkgroup")
@Slf4j
public class CheckGroupController {

    // 注入服务
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("新增检查组：{}", checkGroup);
        try {
            checkGroupService.add(checkGroup, checkitemIds);
            return new Result(true, "新增检查组成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增检查组失败");
        }
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result delete(Integer id){
        log.info("删除检查组：{}", id);
        try {
            return checkGroupService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除检查组失败，请确认！");
        }
    }

    /**
     * 根据套餐id查询检查组id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        log.info("根据套餐id查询检查组id：{}", setmealId);
        try {
            return new Result(true, "查询检查组成功", checkGroupService.findCheckGroupIdsBySetmealId(setmealId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询检查组失败，请确认！");
        }
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("编辑检查组：{}", checkGroup);
        try {
            return checkGroupService.edit(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑检查组失败，请确认！");
        }
    }

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询检查组：{}", queryPageBean);
        return checkGroupService.findPage(queryPageBean);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查组");
        try {
            return new Result(true, "查询成功", checkGroupService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询检查组：{}", id);
        try {
            return new Result(true, "查询成功", checkGroupService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
}
